
/*
 * ChatApplicationPanel.java
 *
 * Created on 27 juin 2010, 09:35:19
 */

package darknet.application.chat.gui;

import darknet.application.chat.impl.ChannelDistantImpl;
import darknet.application.chat.spec.Channel;
import darknet.application.chat.spec.ChatApplication;
import darknet.application.chat.spec.ChatObserver;
import darknet.application.chat.spec.Session;
import java.io.IOException;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Yoyo
 */
public class ChatApplicationPanel extends JPanel implements ChatObserver {

    // TODO Do somethings to make a connection between an application and his UI

    /** Serial version. */
    private static final long serialVersionUID = 1L;

    /** Panel channels list */
    private static final String PANEL_CHANNELS_LIST = "Channels list";

    /** Chat application */
    private ChatApplication application = null;

    /** All panels */
    private Map<String, JPanel> panels = null;

    /**
     * Creates new form ChatApplicationPanel
     * 
     * @param application
     */
    public ChatApplicationPanel(ChatApplication application) {

        if (application == null) {
	    throw new IllegalArgumentException("application is null");
	}
        this.application = application;

        panels = new HashMap<String, JPanel>();

        initComponents();

        // Channels list pannel
        panels.put(PANEL_CHANNELS_LIST,
                new ListChannelsPanel(this.application, this));
        tabPane.addTab(PANEL_CHANNELS_LIST, panels.get(PANEL_CHANNELS_LIST));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabPane = new javax.swing.JTabbedPane();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabPane;
    // End of variables declaration//GEN-END:variables

    @Override
    public void newSessionStart(Session session) {

        if (session == null) {
            throw new IllegalArgumentException("session is invalid (null)");
        }

        // session already tab ?
        if (panels.containsKey(session.getNode().getName())) {
            return;
        }

        // Create the panel.
        SessionPanel panel = new SessionPanel(session, this);
        session.addSessionObserver(panel);
        
        // Put the panel to the panels collection
        panels.put(session.getNode().getName(), panel);

        // tab the panel
        tabPane.addTab(session.getNode().getName(), panel);
    }

    @Override
    public void newSessionExit(Session session) {
        tabPane.remove(panels.get(session.getNode().getName()));
        panels.remove(session.getNode().getName());
    }

    @Override
    public void newChannelStart(Channel channel) {
        
        if (channel == null) {
            throw new IllegalArgumentException("channel is invalid (null)");
        }

        // send the join message to the hoster
        // and add the channel to the list of joined channels
        try {
            channel.join();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(
                    null, ex.getLocalizedMessage(), "Exception",
                    JOptionPane.WARNING_MESSAGE);
        }


        // channel already tab ?
        if (panels.containsKey(channel.getName())) {
            return;
        }

        // Create the panel.
        ChannelPanel panel = new ChannelPanel(channel, this);
        channel.addChannelObserver(panel);
        
        // Put the panel to the panels collection
        panels.put(channel.getName(), panel);

        // tab the panel
        tabPane.addTab(channel.getName(), panel);
    }

    @Override
    public void newChannelExit(Channel channel) {
        
        tabPane.remove(panels.get(channel.getName()));
        panels.remove(channel.getName());

        // update the channel list
        ((ListChannelsPanel) panels.get(PANEL_CHANNELS_LIST))
                .updateChannelList();
    }
}
